package com.work.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.work.user.dao.MenuMapper;
import com.work.user.pojo.Menu;
import com.work.user.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    //多条件搜索
    @Override
    public List<Menu> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        List<Menu> menus = menuMapper.selectByExample(example);
        return menus;
    }

    //条件+分页查询
    @Override
    public Page<Menu> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        return (Page<Menu>)menuMapper.selectAll();
    }

    //多条件分页查询
    @Override
    public Page<Menu> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        List<Menu> menus = menuMapper.selectByExample(example);
        return (Page<Menu>) menus;
    }

    //查询全部权限
    @Override
    public List<Menu> findAll() {
        return menuMapper.selectAll();
    }

    //根据id查询权限
    @Override
    public Menu findById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    //新增权限菜单
    @Override
    public void add(Menu menu) {
        menuMapper.insertSelective(menu);
    }

    //修改权限信息
    @Override
    public void update(Menu menu) {
        menuMapper.updateByPrimaryKey(menu);
    }

    //删除权限
    @Override
    public void delete(Integer id) {
        int count = menuMapper.findByCount(id);
        if (count > 0){
            throw new RuntimeException("请先删除相关角色，再来删除权限！");
        }
        menuMapper.deleteByPrimaryKey(id);
    }

    //构建查询对象
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 权限名
            if(searchMap.get("menu")!=null && !"".equals(searchMap.get("menu"))){
                criteria.andLike("menu","%"+searchMap.get("menu")+"%");
            }
            // 信息
            if(searchMap.get("info")!=null && !"".equals(searchMap.get("info"))){
                criteria.andLike("info","%"+searchMap.get("info")+"%");
            }
            //地址
            if(searchMap.get("url")!=null && !"".equals(searchMap.get("url"))){
                criteria.andLike("url","%"+searchMap.get("url")+"%");
            }
            // 上级菜单
            if(searchMap.get("pid")!=null && !"".equals(searchMap.get("pid"))){
                criteria.andLike("pid","%"+searchMap.get("pid")+"%");
            }
            // 备注
            if(searchMap.get("type")!=null && !"".equals(searchMap.get("type"))){
                criteria.andLike("type","%"+searchMap.get("type")+"%");
            }
            // 菜单管理
            if(searchMap.get("category")!=null && !"".equals(searchMap.get("category"))){
                criteria.andEqualTo("category",searchMap.get("category"));
            }
        }
        return example;
    }
}
